# -*- coding:utf-8 -*-
# editor: zzh
# date: 2022/9/26

import pickle
import json

def sort_by_value(d):
    items=d.items()
    backitems=[[v[1],v[0]] for v in items]
    backitems.sort()
    return {backitems[i][1]:d[backitems[i][1]] for i in range(-1,-len(backitems)-1,-1)}


datas = json.load(open('test_500.json','r',encoding='utf8'))
#
intent2ents = {}
ents2ids = {'NA':0}
#
#
#

propnums = {}

for d in datas:
    # print(d)
    dias = d['dialogue']
    for sentence in dias:
        act = sentence['act']
        if act not in intent2ents:
            intent2ents[act] = dict()

        for info in sentence['info']:
            prop = info['prop']

            if prop not in propnums:
                propnums[prop] = 0
            propnums[prop] += 1

            if prop not in ents2ids:
                ents2ids[prop] = len(ents2ids)

            if prop not in intent2ents[act]:
                intent2ents[act][prop] = 0

            intent2ents[act][prop]+=1
#
#
#
# print(ents2ids)
print(sort_by_value(propnums))

for k in intent2ents:
    K = intent2ents[k]
    print(k, sort_by_value(K))

print(ents2ids)

print(len(datas))

# pickle.dump(ents2ids, open('ents3ids.pkl','wb'))
# pickle.dump(intent2ents, open('intent2ents.pkl','wb'))
#
#
#
# ents2ids = pickle.load(open('ents3ids.pkl','rb'))
# intent2ents = pickle.load(open(r'D:\MyProject\pythonProject\data\TrainingData\tri_prop.pkl','rb'))
# intent2ents = sorted(intent2ents, key=lambda x:x[0])
# print(intent2ents)
# for k in intent2ents:
#     print(k, sorted(intent2ents[k]))
#
# print('****'*20)
#
# i1 = pickle.load(open(r'D:\MyProject\pythonProject\data\intent2ents.pkl','rb'))
# # i1 = sorted(i1, key=lambda x:x[0])
# # print(i1)
# for i in i1:
#     print(i,i1[i])


#######################

# datas = json.load(open('test_500.json','r',encoding='utf8'))
#
# sentence_len = 0
#
# sentence_len_with_prop = 0
#
# for d in datas:
#     # print(d)
#     dias = d['dialogue']
#     sentence_len += len(dias)
#     for sentence in dias:
#         if(len(sentence['info']) != 0):
#             sentence_len_with_prop += 1
#
# print(sentence_len)
# print(sentence_len_with_prop)